﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace PHP
{
	public class FormSetingStore
	{
		private string regPath;
		private string formName;
		private RegistryKey key;

		private string RegistryPath
		{
			get { return regPath; }
		}
		public string FormName
		{
			get { return formName; }
		}
		public FormSetingStore(string registryPath, string formName)
		{
			this.regPath = registryPath;
			this.formName = formName;
			try
			{
				// tao khoa neu no chua ton tai
				key = Registry.CurrentUser.CreateSubKey(registryPath + formName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message.ToString());
				key = null;
			}
		}
		public void SaveSettings(System.Windows.Forms.Form form)
		{
			if (key != null)
			{
				if ((int)form.WindowState == (int)FormWindowState.Normal)
				{
					key.SetValue("Height", form.Height);
					key.SetValue("Width", form.Width);
					key.SetValue("Left", form.Left);
					key.SetValue("Top", form.Top);
				}
				key.SetValue("WindowState", (int)form.WindowState);
			}
		}
		public void ApplySetings(System.Windows.Forms.Form form)
		{
			if (key != null)
			{
				form.Height = (int)key.GetValue("Height", form.Height);
				form.Width = (int)key.GetValue("Width", form.Width);
				form.Left = (int)key.GetValue("Left", form.Left);
				form.Top = (int)key.GetValue("Top", form.Top);
				form.WindowState = (FormWindowState)key.GetValue("WindowState", form.WindowState);
			}
		}
	}
}
